# script for topic 7o # # first do the problem the long way # get the midpoints and the frequencies mid_p <- seq(15,57,7)+3.5 mid_p freqs <- c(15,46,54, 25, 45, 74, 16) freqs # find the total number of data values num_x <- sum( freqs ) num_x # get the sum of the midpoints times # the count (the value in freqs) sum_x <- sum( mid_p * freqs) sum_x # then the mean is sum_x/num_x mu <- sum_x/num_x mu # now to get the standard deviation # first create squares of the midpoints mp_sq <- mid_p^2 mp_sq # then find the sum of the products of # the mp_sq and the counts sum_xsq <- sum( mp_sq * freqs) sum_xsq # finally use the alternative formula # to get the standard deviation std_dev = sqrt( (sum_xsq - (sum_x^2)/num_x)/num_x) std_dev # # then there is the shorter way # assuming that we know the midpoints and # the frequencies (lines 5-8 above ) # just make the data values x <- rep( mid_p, freqs ) head(x, 19) tail(x, 19) # then just find the mean and # standard deviation of x mean( x ) source("../pop_sd.R") pop_sd(x) # and if this was a sample sd(x) # # Then there is the much shorter way # again assuming that we did lines # 7 and 8 source("../get_from_table.R") get_from_table( 15, 22, freqs)